/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ruoyi.common.utils.equipment;

/**
 *
 * @author Administrator
 */
public class HEXUtil {

    private final static char[] ByteToHex_Digit = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
    private final static byte[] HexToByte_Digit;

    //初始化 HexToByte 的转换表
    static {
        int i;
        byte[] digits = new byte[256];
        byte[] tmp;
        tmp = "0123456789abcdef".getBytes();
        for (i = 0; i < tmp.length; i++) {
            digits[tmp[i]] = (byte) i;
        }
        tmp = "ABCDEF".getBytes();
        for (i = 0; i < tmp.length; i++) {
            digits[tmp[i]] = (byte) (i + 10);
        }
        HexToByte_Digit = digits;
    }

    /**
     * 检查字符串是否为空字符串或者为null
     *
     * @param str 需要检查的字符串
     * @return true表示为空或null
     */
    public static boolean IsNullOrEmpty(String str) {
        if (str == null) {
            return true;
        }
        if (str.length() == 0) {
            return true;
        }
        return false;
    }

    /**
     * 检查是否为十六进制字符串
     *
     * @param hexString 需要检查的字符串
     * @return true 表示是十六进制，false 表示包含非十六进制字符
     */
    public static boolean IsHex(String hexString) {
        if (IsNullOrEmpty(hexString)) {
            return false;
        }
        byte[] sbuf = hexString.getBytes();
        byte[] digits = HexToByte_Digit;
        int i, ilen = sbuf.length;
        //开始转换
        for (i = 0; i < ilen; i++) {
            if (digits[sbuf[i] & 0x000000ff] == 0 && sbuf[i] != 0x30) {
                return false;
            }
        }
        return true;
    }

    /**
     * 十六进制字符串转字节数组
     *
     * @param hexString 需要转换的十六进制字符串
     * @return 转换后的字节数组 如果字符串为null则返回null
     */
    public static byte[] HexToByte(String hexString) {
        int i, iIndex = 0, iData;
        if (IsNullOrEmpty(hexString)) {
            return null;
        }
        if (!IsHex(hexString)) {
            return null;
        }

        //确定字符串长度必须是2的倍数
        if ((hexString.length() % 2) == 1) {
            hexString = "0" + hexString;
        }
        //生成转换值列表
        byte[] digits = HexToByte_Digit;

        //生成缓存
        byte[] buf = new byte[hexString.length() / 2];
        byte[] sbuf = hexString.getBytes();
        int ilen = sbuf.length;
        //开始转换
        for (i = 0; i < ilen; i++) {
            iData = digits[sbuf[i++] & 0x000000ff] * 16;
            iData = iData + digits[sbuf[i]];

            buf[iIndex] = (byte) iData;
            iIndex++;
        }

        return buf;
    }

    /**
     * 十六进制字符串转字节数组
     *
     * @param hexString 需要转换的十六进制字符串
     * @return 转换后的字节数组 如果字符串为null则返回null
     */
    public static byte[] HexToByte(String hexString,int len) {
        int i, iIndex = 0, iData;
        if (IsNullOrEmpty(hexString)) {
            return null;
        }
        if (!IsHex(hexString)) {
            return null;
        }

        //确定字符串长度必须是2的倍数
        if ((hexString.length() % 2) == 1) {
            hexString = "0" + hexString;
        }
        //生成转换值列表
        byte[] digits = HexToByte_Digit;

        //生成缓存
        byte[] buf = new byte[hexString.length() / 2];

        byte[] sbuf = hexString.getBytes();
        int ilen = sbuf.length;

        //开始转换
        for (i = 0; i < ilen; i++) {
            iData = digits[sbuf[i++] & 0x000000ff] * 16;
            iData = iData + digits[sbuf[i]];

            buf[iIndex] = (byte) (0XFF&iData) ;
            iIndex++;
        }
        if(ilen<len){
            int b=len-buf.length;
            byte[] bb = new byte[b];
            for (i = 0; i < b; i++){
                bb[i]=0;
            }
            byte[] aa=new byte[bb.length+buf.length];
            System.arraycopy(bb,0,aa,0,bb.length);
            System.arraycopy(buf,0,aa,bb.length,buf.length);
            return aa;
        }
        return buf;
    }
    public static byte[] int2ByteArray(int i){
               byte[] result=new byte[4];
               result[0]=(byte)((i >> 24)& 0xFF);
               result[1]=(byte)((i >> 16)& 0xFF);
               result[2]=(byte)((i >> 8)& 0xFF);
               result[3]=(byte)(i & 0xFF);
               return result;
           }
    /**
     * Byte到Hex的转换
     *
     * @param b 需要转换的字节数组，转换的长度
     * @return 十六进制字符串
     */
    public static String ByteToHex(byte[] b) {
        if (b == null) {
            return null;
        }
        if (b.length == 0) {
            return null;
        }

        int ilen = b.length;
        char[] sHexbuf = new char[ilen * 2];
        int lIndex = 0;
        int iData;
        char[] digits = ByteToHex_Digit;
        try {
            for (int i = 0; i < ilen; i++) {
                iData = (b[i] & 0x000000ff);//取字节的无符号整形数值

                sHexbuf[lIndex++] = digits[iData / 16];
                sHexbuf[lIndex++] = digits[iData % 16];
            }
            return new String(sHexbuf).toUpperCase();
        } catch (Exception e) {
            return new String();
        }
    }

    /**
     * CRC8校验,多项式0X31,LSB First，初始值0X00
     *
     * @param ptr 要校验的数组
     * @param len 数组长度
     * @return CRC8码
     */
    public static int Get_CRC8(byte[] ptr, int len) {
        int crc;
        int i;
        crc = 0;
        int iIndex = 0;
        while ((len--) > 0) {
            crc ^= (ptr[iIndex] & 0x000000ff);
            for (i = 0; i < 8; i++) {
                if ((crc & 0x01) == 1) {
                    crc = (crc >> 1) ^ 0x8C;
                } else {
                    crc >>= 1;
                }
            }
            iIndex++;
        }
        return crc;
    }

}
